package epam.com.myThreads;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Runner {

	private static int countPlaces;
	private static int countAutos;
	private static int countCycles;
	public static List<Auto> Autos = new ArrayList<Auto>();
	private static Properties props = new Properties();

	private static void loadProperties() throws FileNotFoundException,
			IOException {		
		props = new Properties();
		props.load(new FileInputStream("config.properties"));
		countPlaces = Integer.parseInt(props.getProperty("countPlaces"));
		countAutos = Integer.parseInt(props.getProperty("countAutos"));
		countCycles = Integer.parseInt(props.getProperty("countCycles"));
	}

	public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {

		loadProperties();	
		
		Places places = new Places(countPlaces);	
		
		for (int i = 1; i <= countPlaces; i++) {
			places.addPlace(new Place("place " +i ));
		}

		for (int i = 1; i <= countAutos; i++) {
			Autos.add(new Auto("car " + i, places));
		}

		for (int i = 0; i < countCycles; i++) {
			for (Auto item : Autos) {
				item.threadAuto = new Thread(item);
				item.threadAuto.start();
			}
			for (Auto item : Autos) {
				item.threadAuto.join();
			}
		}

	}

}
